$(document).ready(function() {
    source.init();
});

var source = (function() {
    function addSource(categoryCode){
        var url= $('#sourceURL_' + categoryCode).val();
//            categoryCode = $('input[name=categoryCode]').attr('value');
        $.post('/source/ajax-add', {
            url:url,
            categoryCode: categoryCode
        }, function(resp) {
            if(resp.err === 0){
            }else {
            }
        }, 'json');  
    }
    
    function deleteSource(sourceId, sourceUrl, callback){
        showConfirm({
            msg: 'Bạn có chắc chắn muốn xoá tin này không? <strong class="text-danger">' + sourceUrl +'</strong>',
            title: 'Xoá Tin tức',
            noBt: 'Đóng',
            yesBt: 'Đồng ý xoá',
            onYes: function() { 
                $.post('/source/ajax-delete', {id: sourceId}, function(resp) {
                    if(resp.err === 0){
                        callback();
                        hideConfirm();
                    }else {
                         hideConfirm();
                        $('.errorHandler').removeClass('text-success').addClass('text-danger').html(resp.msg).show();
                    }
                }, 'json');
            }
        });
    }
    function initHanlder(){
 
        $('.category_item').click(function(e) {
            e.preventDefault();
            $('tr.info').removeClass('info');
            $(this).parents('tr').first().addClass('info');
            $('#current_content').html($(this).parents('tr').first().find('.post_content').html());
            var code =  $(this).attr('rel');
            $('.listCate').addClass('hide');
            var currentCate = $('#listCate_' + code);
            $(currentCate).removeClass('hide');
            $('input[name=categoryCode_'+code+']').attr('value',code);
            return false;
        });

        $('.addSource').click(function() {
            var code = $(this).attr('rel');
            addSource(code);
            return false;
        });
        $('#deletePost').click(function() {
            var pid = $('#postPid').val(),
                id = $('#postId').val(),
                title = $('#postTitle').val();
            deletePost(pid, id, title, function() {
                window.location.href='/profile/edit?id=' + pid;
            });
            return false;
        });
        $('#btn-delete-selected').click(function() {
            var toDel =[];
            $('input.cbDelete:checked').each(function(i,v){
                var id = $(this).attr('id');
                id = id.substring(4);
                toDel.push(id);
            });
            var path = 'post'
            var url = window.location.href;
            if(url.indexOf('cnews')> 0) path = 'cnews';
            $.post('/'+path+'/ajax-delete-multiple', {ids:JSON.stringify(toDel)}, function(resp) {
                    if(resp.err === 0){
                        alert("Đã xóa!");
                    }else {
                        $('.errorHandler').removeClass('text-success').addClass('text-danger').html(resp.msg).show();
                    }
                }, 'json');
            return false;
        });
        var hash = window.location.hash;
        if(hash.indexOf('#msg=') > -1){
            $('.errorHandler').html(hash.substring(5)).show();
            window.location.hash = '';
        }
    }
    return {
        init: function() {
            initHanlder();
            initDatePicker('#postTime');
        },
        
        deleteSource: function(obj, sourceId){
            var sourceUrl = $(obj).find('td a.url').text();
            deleteSource(sourceId, sourceUrl, function(){
                $(obj).remove();
            });
            return false;
        }
    };
})();